#include <atomic>
#include <iostream>
#include <thread>
#include <vector>

std::atomic<int> x(0);
std::atomic<bool> go(false);

void thread_function(int id) {
    while (!go.load(std::memory_order_seq_cst)) {
        // 循环等待，直到主线程发出开始信号
    }
    // 安全地进行计算或状态更新，知道所有线程都将看到相同的操作顺序
    int value = x.load(std::memory_order_seq_cst);
    std::cout << "Thread " << id << " sees x = " << value << std::endl;
}

int main() {
    std::vector<std::thread> threads;
    for (int i = 0; i < 5; ++i) {
        threads.emplace_back(thread_function, i);
    }

    // 准备数据
    x.store(1, std::memory_order_seq_cst);
    // 通知所有线程开始执行
    go.store(true, std::memory_order_seq_cst);

    for (auto& t : threads) {
        t.join();
    }

    return 0;
}